using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;

namespace MatrixFramework.Shared.Domain.Users
{
    #region AppUserClientScope - 用户客户端Scope关联

    /// <summary>
    /// 用户客户端Scope关联
    /// </summary>
    public class AppUserClientScope : CreationAuditedEntity<Guid>
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 客户端ID
        /// </summary>
        [Required]
        [StringLength(128)]
        public string ClientId { get; set; }

        /// <summary>
        /// Scope名称
        /// </summary>
        [Required]
        [StringLength(128)]
        public string ScopeName { get; set; }

        /// <summary>
        /// 授权时间
        /// </summary>
        public DateTime GrantedAt { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpiresAt { get; set; }

        /// <summary>
        /// 是否活跃
        /// </summary>
        public bool IsActive { get; set; } = true;

        protected AppUserClientScope()
        {
        }

        public AppUserClientScope(
            Guid userId,
            string clientId,
            string scopeName)
        {
            UserId = userId;
            ClientId = clientId;
            ScopeName = scopeName;
            GrantedAt = DateTime.UtcNow;
            IsActive = true;
        }
    }

    #endregion

    #region UserPermissionScope - 用户权限范围

    /// <summary>
    /// 用户权限范围
    /// </summary>
    public class UserPermissionScope : CreationAuditedEntity<Guid>
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 资源ID
        /// </summary>
        public Guid ResourceId { get; set; }

        /// <summary>
        /// 资源名称
        /// </summary>
        [Required]
        [StringLength(128)]
        public string ResourceName { get; set; }

        /// <summary>
        /// 权限列表
        /// </summary>
        public virtual List<string> Permissions { get; set; } = new();

        /// <summary>
        /// 授权时间
        /// </summary>
        public DateTime GrantedAt { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpiresAt { get; set; }

        /// <summary>
        /// 是否活跃
        /// </summary>
        public bool IsActive { get; set; } = true;

        protected UserPermissionScope()
        {
        }

        public UserPermissionScope(
            Guid userId,
            Guid resourceId,
            string resourceName,
            List<string> permissions)
        {
            UserId = userId;
            ResourceId = resourceId;
            ResourceName = resourceName;
            Permissions = permissions ?? new List<string>();
            GrantedAt = DateTime.UtcNow;
            IsActive = true;
        }
    }

    #endregion

    #region UserTwoFactorRecoveryCode - 用户双因子认证备用码

    /// <summary>
    /// 用户双因子认证备用码
    /// </summary>
    public class UserTwoFactorRecoveryCode : CreationAuditedEntity<Guid>
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 备用码
        /// </summary>
        [Required]
        [StringLength(16)]
        public string Code { get; set; }

        /// <summary>
        /// 是否已使用
        /// </summary>
        public bool IsUsed { get; set; }

        /// <summary>
        /// 使用时间
        /// </summary>
        public DateTime? UsedAt { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime ExpiresAt { get; set; }

        protected UserTwoFactorRecoveryCode()
        {
        }

        public UserTwoFactorRecoveryCode(
            Guid userId,
            string code,
            DateTime expiresAt)
        {
            UserId = userId;
            Code = code;
            ExpiresAt = expiresAt;
            IsUsed = false;
        }

        /// <summary>
        /// 标记为已使用
        /// </summary>
        public void MarkAsUsed()
        {
            IsUsed = true;
            UsedAt = DateTime.UtcNow;
        }
    }

    #endregion

    #region UserPreferences - 用户偏好设置

    /// <summary>
    /// 用户偏好设置
    /// </summary>
    public class UserPreferences
    {
        /// <summary>
        /// 主题设置
        /// </summary>
        public string Theme { get; set; } = "default";

        /// <summary>
        /// 语言设置
        /// </summary>
        public string Language { get; set; } = "zh-CN";

        /// <summary>
        /// 时区设置
        /// </summary>
        public string TimeZone { get; set; } = "UTC";

        /// <summary>
        /// 日期格式
        /// </summary>
        public string DateFormat { get; set; } = "yyyy-MM-dd";

        /// <summary>
        /// 时间格式
        /// </summary>
        public string TimeFormat { get; set; } = "HH:mm:ss";

        /// <summary>
        /// 数字格式
        /// </summary>
        public string NumberFormat { get; set; } = "en-US";

        /// <summary>
        /// 是否启用通知
        /// </summary>
        public bool EnableNotifications { get; set; } = true;

        /// <summary>
        /// 是否启用邮件通知
        /// </summary>
        public bool EnableEmailNotifications { get; set; } = true;

        /// <summary>
        /// 是否启用短信通知
        /// </summary>
        public bool EnableSmsNotifications { get; set; } = false;

        /// <summary>
        /// 是否启用推送通知
        /// </summary>
        public bool EnablePushNotifications { get; set; } = true;

        /// <summary>
        /// 桌面通知设置
        /// </summary>
        public Dictionary<string, bool> DesktopNotifications { get; set; } = new();

        /// <summary>
        /// 邮件通知设置
        /// </summary>
        public Dictionary<string, bool> EmailNotifications { get; set; } = new();

        /// <summary>
        /// 短信通知设置
        /// </summary>
        public Dictionary<string, bool> SmsNotifications { get; set; } = new();

        /// <summary>
        /// 推送通知设置
        /// </summary>
        public Dictionary<string, bool> PushNotifications { get; set; } = new();

        /// <summary>
        /// 界面设置
        /// </summary>
        public Dictionary<string, object> UiSettings { get; set; } = new();

        /// <summary>
        /// 工作区设置
        /// </summary>
        public Dictionary<string, object> WorkspaceSettings { get; set; } = new();

        /// <summary>
        /// 自定义设置
        /// </summary>
        public Dictionary<string, object> CustomSettings { get; set; } = new();

        public UserPreferences()
        {
            // 初始化通知设置
            InitializeNotificationSettings();
        }

        private void InitializeNotificationSettings()
        {
            // 默认通知类型
            var notificationTypes = new[]
            {
                "system", "security", "workflow", "reminder", "deadline",
                "mention", "comment", "approval", "task", "message"
            };

            foreach (var type in notificationTypes)
            {
                DesktopNotifications[type] = true;
                EmailNotifications[type] = true;
                SmsNotifications[type] = false;
                PushNotifications[type] = true;
            }
        }

        /// <summary>
        /// 更新偏好设置
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void UpdatePreference(string key, object value)
        {
            var property = typeof(UserPreferences).GetProperty(key);
            if (property != null && property.CanWrite)
            {
                property.SetValue(this, value);
            }
            else
            {
                CustomSettings[key] = value;
            }
        }

        /// <summary>
        /// 获取偏好设置
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>设置值</returns>
        public T GetPreference<T>(string key, T defaultValue = default)
        {
            var property = typeof(UserPreferences).GetProperty(key);
            if (property != null && property.CanRead)
            {
                return (T)property.GetValue(this);
            }

            if (CustomSettings.TryGetValue(key, out var value) && value is T typedValue)
            {
                return typedValue;
            }

            return defaultValue;
        }
    }

    #endregion

    #region RolePermission - 角色权限关联

    /// <summary>
    /// 角色权限关联
    /// </summary>
    public class RolePermission : CreationAuditedEntity<Guid>
    {
        /// <summary>
        /// 角色ID
        /// </summary>
        public Guid RoleId { get; set; }

        /// <summary>
        /// 权限名称
        /// </summary>
        [Required]
        [StringLength(256)]
        public string PermissionName { get; set; }

        /// <summary>
        /// 是否授予
        /// </summary>
        public bool IsGranted { get; set; }

        /// <summary>
        /// 授权时间
        /// </summary>
        public DateTime GrantedAt { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpiresAt { get; set; }

        /// <summary>
        /// 是否活跃
        /// </summary>
        public bool IsActive { get; set; } = true;

        protected RolePermission()
        {
        }

        public RolePermission(
            Guid roleId,
            string permissionName,
            bool isGranted = true)
        {
            RoleId = roleId;
            PermissionName = permissionName;
            IsGranted = isGranted;
            GrantedAt = DateTime.UtcNow;
            IsActive = true;
        }
    }

    #endregion

    #region RoleDataPermissionDepartment - 角色数据权限部门关联

    /// <summary>
    /// 角色数据权限部门关联
    /// </summary>
    public class RoleDataPermissionDepartment : CreationAuditedEntity<Guid>
    {
        /// <summary>
        /// 角色ID
        /// </summary>
        public Guid RoleId { get; set; }

        /// <summary>
        /// 部门ID
        /// </summary>
        public Guid DepartmentId { get; set; }

        /// <summary>
        /// 授权时间
        /// </summary>
        public DateTime GrantedAt { get; set; }

        /// <summary>
        /// 是否包含子部门
        /// </summary>
        public bool IncludeSubDepartments { get; set; } = true;

        /// <summary>
        /// 是否活跃
        /// </summary>
        public bool IsActive { get; set; } = true;

        protected RoleDataPermissionDepartment()
        {
        }

        public RoleDataPermissionDepartment(
            Guid roleId,
            Guid departmentId,
            bool includeSubDepartments = true)
        {
            RoleId = roleId;
            DepartmentId = departmentId;
            IncludeSubDepartments = includeSubDepartments;
            GrantedAt = DateTime.UtcNow;
            IsActive = true;
        }
    }

    #endregion

    #region RoleClientMapping - 角色客户端映射

    /// <summary>
    /// 角色客户端映射
    /// </summary>
    public class RoleClientMapping : CreationAuditedEntity<Guid>
    {
        /// <summary>
        /// 角色ID
        /// </summary>
        public Guid RoleId { get; set; }

        /// <summary>
        /// 客户端ID
        /// </summary>
        [Required]
        [StringLength(128)]
        public string ClientId { get; set; }

        /// <summary>
        /// 客户端角色名称
        /// </summary>
        [Required]
        [StringLength(128)]
        public string ClientRoleName { get; set; }

        /// <summary>
        /// 映射时间
        /// </summary>
        public DateTime MappedAt { get; set; }

        /// <summary>
        /// 是否同步到Keycloak
        /// </summary>
        public bool IsSyncedToKeycloak { get; set; }

        /// <summary>
        /// Keycloak同步时间
        /// </summary>
        public DateTime? KeycloakSyncTime { get; set; }

        /// <summary>
        /// 是否活跃
        /// </summary>
        public bool IsActive { get; set; } = true;

        protected RoleClientMapping()
        {
        }

        public RoleClientMapping(
            Guid roleId,
            string clientId,
            string clientRoleName)
        {
            RoleId = roleId;
            ClientId = clientId;
            ClientRoleName = clientRoleName;
            MappedAt = DateTime.UtcNow;
            IsSyncedToKeycloak = false;
            IsActive = true;
        }

        /// <summary>
        /// 标记为已同步到Keycloak
        /// </summary>
        public void MarkAsSyncedToKeycloak()
        {
            IsSyncedToKeycloak = true;
            KeycloakSyncTime = DateTime.UtcNow;
        }
    }

    #endregion
}